﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuxiWTO.Mapping;
using WutiWTO.Common;

namespace WuxiWTO.Web
{
    public partial class updateInfo : System.Web.UI.Page
    {
        WTODataContext ctx = new WTODataContext();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user_name"] == null && Request.Cookies["wtouser"] == null)
            {
                Response.Redirect(Server.HtmlEncode("login.aspx?ret=updateInfo.aspx"));
            }
            if (!IsPostBack)
            {
                supporter user = ctx.supporters.SingleOrDefault(p => p.supporter_login_name == Session["user_name"].ToString());
                if (user != null)
                {
                    if (UserInfo.AllowRegValidate())
                    {
                        if (user.is_active != 'F')
                        {
                            Response.Redirect(Server.HtmlEncode("userCenter.aspx"));
                        }
                        else
                        {
                            this.lblMessage.Text = user.validate_reason;

                            FillTheControl(user);
                        }
                    }
                    else
                    {
                        FillTheControl(user);
                    }
                }
            }
        }

        private void FillTheControl(supporter user)
        {
            this.txtLoginNameP.Text = user.supporter_login_name;
            this.txtEmailP.Text = user.email;
            this.txtNameP.Text = user.enterprise_name;
            this.txtContact.Text = user.contact;
            this.txtTelephoneP.Text = user.mobile;
            this.txtPosition.Text = user.position;
            this.txtAddress.Text = user.enterprise_address;
        }



        protected void btnOK_Click(object sender,EventArgs e)
        {
             supporter user = ctx.supporters.SingleOrDefault(p => p.supporter_login_name == Session["user_name"].ToString());
             if (user != null)
             {
                 user.enterprise_name = this.txtNameP.Text.Trim();
                 user.mobile = this.txtTelephoneP.Text.Trim();
                 if (UserInfo.AllowRegValidate())
                 {
                     user.is_active = 'N';
                 }
                 else
                 {
                     user.is_active = 'Y';
                 }
                 user.contact = this.txtContact.Text.Trim();
                 user.position = this.txtPosition.Text.Trim();
                 user.enterprise_address = this.txtAddress.Text.Trim();
                 user.validate_reason = "";
                 ctx.SubmitChanges();
                 Response.Redirect("userCenter.aspx");
             }
        }
    }
}